package com.asdf.ui;

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;

public class CreditCardDetailsDialog extends Dialog
{

	private static final long	serialVersionUID	= 7308823390935040567L;

	private static final String	CC_NAME_LABEL_CAPTION		= "Credit card company:";
	private static final String	CC_NUMBER_LABEL_CAPTION		= "Card number:";
	private static final String	CC_EXP_MONTH_LABEL_CAPTION	= "Expiration month:";
	private static final String	CC_EXP_YEAR_LABEL_CAPTION	= "Expiration year:";
	private static final String ZIP_LABEL_CAPTION      	 	= "Zip code:";

	private static final String	OK		= "OK";
	private static final String	CANCEL	= "Cancel";

	private JPanel inputPanel  = new JPanel( new GridLayout( 0, 2));
	private JPanel buttonPanel = new JPanel( new FlowLayout( FlowLayout.CENTER)); 
	
	private JLabel 		lblZip = new JLabel( ZIP_LABEL_CAPTION);
	private JTextField  txtZip = new JTextField( );
	
	private JLabel lblCardCompany 	= new JLabel( CC_NAME_LABEL_CAPTION);
	private JLabel lblCardNumber  	= new JLabel( CC_NUMBER_LABEL_CAPTION);
	private JLabel lblExpMonth   	= new JLabel( CC_EXP_MONTH_LABEL_CAPTION);
	private JLabel lblExpYear		= new JLabel( CC_EXP_YEAR_LABEL_CAPTION);
	
	private JTextField txtCardCompany 	= new JTextField( );
	private JTextField txtCardNumber 	= new JTextField( );
	private JTextField txtExpMonth		= new JTextField( );
	private JTextField txtExpYear		= new JTextField( );

	JButton btnOK = new JButton( OK);
	JButton btnCancel = new JButton( CANCEL);
		
	protected void drawLabelsAndInputs( )
	{				
		buttonPanel.add( btnOK);
		buttonPanel.add( btnCancel);
		
		inputPanel.add( lblCardCompany);
		inputPanel.add( txtCardCompany);
		
		inputPanel.add( lblCardNumber);
		inputPanel.add( txtCardNumber);
		
		inputPanel.add( lblExpMonth);
		inputPanel.add( txtExpMonth);
		
		inputPanel.add( lblExpYear);
		inputPanel.add( txtExpYear);
		
		lblZip.setLabelFor( txtZip);
		inputPanel.add( lblZip);
		inputPanel.add( txtZip);
		
		TitledBorder title = BorderFactory.createTitledBorder( BorderFactory.createLineBorder(Color.black),
				  "Enter your credit card details:", TitledBorder.LEFT, TitledBorder.TOP);

		inputPanel.setBorder( title);
		
		add( inputPanel);
		add( buttonPanel);
	}
	
	public CreditCardDetailsDialog( )
	{
		super( "Credit Card Details");
		drawLabelsAndInputs( );
		pack( ); 
	}
	
	public void registerEventHandlerCancelCommand( String commandName, ActionListener listener)
	{
		btnCancel.setActionCommand( commandName);
		btnCancel.addActionListener( listener);
	}
	
	public void registerEventHandlerOkCommand( String commandName, ActionListener listener)
	{
		btnOK.setActionCommand( commandName);
		btnOK.addActionListener( listener);
	}
	
	public String getCompanyName( )
	{
		return txtCardCompany.getText( );
	}
	
	public String getCardNumber( )
	{
		return txtCardNumber.getText( );
	}
	
	public String getExpirationMonth( )
	{
		return txtExpMonth.getText( );
	}
	
	public String getExpirationYear( )
	{
		return txtExpYear.getText( );
	}

	public String getZip( )
	{
		return txtZip.getText( );
	}
	
	public static void main( String [ ] args)
	{
		CreditCardDetailsDialog dlg = new CreditCardDetailsDialog( );
		dlg.setVisible( true);
	}
}
